package ch.unisi.inf.pfii.teamwhite.risk.graphic.gameFrame.dialogs;

/**
 * 
 * @author Matteo Bellan: bellanm@lu.unisi.ch
 * @author Andrea Michelotti: micheloa@lu.unisi.ch
 * @author Ranganathan Ramasubramanian: ramasubr@lu.unisi.ch
 *
 */

import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JSlider;

import ch.unisi.inf.pfii.teamwhite.risk.core.Game;
import ch.unisi.inf.pfii.teamwhite.risk.core.player.Human;
import ch.unisi.inf.pfii.teamwhite.risk.graphic.gameFrame.GameFrame;

public class NumberOfShipsDialog extends JDialog{
	
	private Game game;
	
	private JLabel numberOfShips;
	
	private String destination;
	
	public NumberOfShipsDialog(final Game game, final String destination, GameFrame gameFrame) {
		super(gameFrame, "Number of ships", true);
		
		this.game = game;
		
		this.destination = destination;
		
		setLayout(new GridLayout(3, 1));

		final JSlider numberOfShipsSlider = new JSlider(1, ((Human) game.getCurrentPlayer()).getSelectedPlanet().getShips().size() - 1, 1);
		numberOfShipsSlider.setMajorTickSpacing(1);
		numberOfShipsSlider.setMinorTickSpacing(1);
		numberOfShipsSlider.setPaintTicks(true);
		numberOfShipsSlider.addMouseListener(new MouseListener() {
			public void mouseReleased(MouseEvent e) {
				numberOfShips.setText("" + numberOfShipsSlider.getValue());
			}
			public void mousePressed(MouseEvent e) {}
			public void mouseExited(MouseEvent e) {}
			public void mouseEntered(MouseEvent e) {}
			public void mouseClicked(MouseEvent e) {}
		});
		add(numberOfShipsSlider);
	    
	    JButton ok = new JButton("Move");
		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int shipsToMove = numberOfShipsSlider.getValue();
			    ((Human)game.getCurrentPlayer()).moveShips(NumberOfShipsDialog.this.destination, shipsToMove);
			    shipsToMove = 1;
			    setVisible(false);
			}
		});
		add(showNumber());
		
		add(ok);
		
		setSize(240, 180);
		setLocationRelativeTo(null);
		setVisible(true);
	    pack();
	}
	
//	This method creates a label which contains the number of selected ships by the slider
	private Component showNumber() {		
		numberOfShips = new JLabel("1");
		numberOfShips.setBorder(BorderFactory.createTitledBorder("NumberOfShips"));
		return numberOfShips;
	}
}
